package com.johnny.kotlinstudy.study1

import java.util.*

/**
 *
 *  Created by Johnny on 2023/3/8
 *  Copyright © 2023年 大事件. All rights reserved.
 *
 *  Describe:
 */
const val TAG = "Johnny"
fun main(args: Array<String>) {
    //todo  let run apply also With takeIf takeUnless 函数

    var str: String? = TAG
//    str = null
//    str = ""
    //let 返回的是最后一行的结果
    str = str?.let { it ->
        //非空字符串
        if (it.isNotBlank()) {
            println("变大写")
            it.uppercase(Locale.getDefault())
        } else {
            TAG
        }
    } ?: "我没有传值呢"//值为 NULL的时候，设置默认值
    println(str)

    val data = TestData(TAG)
    println("变更前=>> ${data.name}")

    data.apply {
        name = "超大杯 Johnny"
        println("apply-> change After=>> ${data.name}")
    }

    data.name = TAG//恢复默认值
    data.run {
        name = "超大杯 Johnny"
        println("run-> change After=>> ${data.name}")
    }

    //also返回接受者对象
    val data2 = TestData(TAG).also {
        println("also-> print=>> ${it.name}")
    }.also {
        it.name = "超大杯 Johnny"
    }
    println("also-> change After=>> ${data.name}")


    //with
    val result1: Boolean = with(TestData(TAG)) {
        name.isNotEmpty()
    }
    println("with-> change After=>> ${result1}")
    val result2: Boolean = with(100) {
        this < 10
    }
    println("with-> change After=>> ${result2}")


    val data3 = TestData(TAG).takeIf {
        //如果判断结果是true,从takeIf函数返回接受者对象，如果false，则返回null,
//        it.name.length==2
        it.name.isNotEmpty()
    } ?: "数据为false了 "
    println("takeIf-> change After=>> ${data3}")


    val data4 = TestData(TAG).takeUnless {
        //如果判断结果是 false,从takeIf函数返回接受者对象，如果true，则返回null,
//        it.name.length==2
        it.name.isNotEmpty()
    } ?: "数据为true了 "
    println("takeUnless-> change After=>> ${data4}")


    println(" 枚举=>> ${Week.MON}")
    println(" 枚举=>> ${Week2.MON.updateTestData(TestData("王五")).name}")
    println(" 枚举=>> ${Week2.TUES.getNowName()}")



}

data class TestData(var name: String)
open class TestData2(var name: String)
enum class Week {
    MON,
    TUES,
    WED,
}

enum class Week2(private val data: TestData) {
    MON(TestData("张三")),
    TUES(TestData("李四")),
    WED(TestData("王五"));

    fun getNowName(): TestData {
        return data
    }

    fun updateTestData(playerCoordinate: TestData) =
        TestData(playerCoordinate.name)
}
